<template>
  <div ref="container" class="aside-right" style="min-width: 200px; position: relative">
    <!-- 拖拽条 -->
    <div v-if="resizeShow" class="aside-right-resize" @mousedown="handleMouseDown"></div>
    <div class="attribute">
      <div>
        <div class="main-attribute">
          <div class="map-info-container">
            <div v-for="(value, key) in this.stateValues" :key="key">
              <label>{{ key }}</label>
              <el-table :data="value" stripe :show-header="false" size="mini">
                <el-table-column label="参数名称" width="120" prop="para_name"></el-table-column>
                <el-table-column width="120" prop="para_value"></el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    // 控制拖拽条的是否显示，默认显示
    resizeShow: {
      type: Boolean,
      default: true,
    },
  },
  mounted() {},
  data() {
    return {
      stateValues: {
        机器人信息: [
          { para_name: "位置(m)", para_value: "0" },
          { para_name: "角度(°)", para_value: "0" },
          { para_name: "线速度(m/s)", para_value: "0.0" },
          { para_name: "角速度(弧度/s)", para_value: "0.0" },
        ],
        // 电机信息: [
        //   { para_name: "电机1", para_value: 0.0 },
        //   { para_name: "电机2", para_value: 0.0 },
        // ],
        // 相机信息: [
        //   { para_name: "相机1", para_value: 0.0 },
        //   { para_name: "相机2", para_value: 0.0 },
        // ],
      },
      startX: 0, // 鼠标开始位置
      moveLen: 0, // 鼠标移动的距离
      startY: 0,
      curLen: 0, // 当前宽度
      otherBoxWidth: 0, // 另一个盒子的宽度
      resizeBox: null, // 拖拽条
      currentBox: null, // 当前盒子
      parentBox: null, // 当前盒子
      rightBox: null, // 右侧盒子
      leftBox: null, // 右侧盒子
    };
  },
  watch: {
    "$store.state.DeviceStatus": function (newValue, oldValue) {
      if (newValue !== "") {
        let count = 0;
        for (const key in this.stateValues) {
          const values = Object.values(this.$store.state.DeviceStatus[key]);
          for (const value of values) {
            this.stateValues[key][count].para_value = value;
            count += 1;
          }
          count = 0;
        }
      }
    },
  },
  methods: {
    getPreviousElement(element) {
      if (element.previousElementSibling) {
        // 如果存在上一个元素兄弟节点，则直接返回
        return element.previousElementSibling;
      } else {
        // 否则，遍历上一个兄弟节点，直到找到一个元素节点
        var prev = element.previousSibling;
        while (prev && prev.nodeType !== 1) {
          // nodeType === 1 表示元素节点
          prev = prev.previousSibling;
        }
        return prev;
      }
    },
    handleMouseDown(e) {
      this.resizeBox = e.target;
      console.log(this.resizeBox);

      this.currentBox = this.resizeBox.parentNode; // 当前盒子
      this.leftBox = this.getPreviousElement(this.currentBox);
      if (!this.leftBox) return;
      this.curLen = this.currentBox.clientWidth;
      this.parentBox = this.currentBox.parentNode;
      this.otherBoxWidth = this.parentBox.clientWidth - this.currentBox.clientWidth - this.leftBox.clientWidth; // 其他盒子的宽度
      console.log(this.otherBoxWidth);
      // 颜色改变提醒
      // this.resizeBox.style.background = "#818181";
      this.startX = e.clientX;
      document.addEventListener("mousemove", this.onMousemove);
      document.addEventListener("mouseup", this.onMouseup);
    },
    onMouseup() {
      // 颜色恢复
      // this.resizeBox.style.background = "#d6d6d6";
      document.removeEventListener("mousedown", this.onMouseDown);
      document.removeEventListener("mousemove", this.onMousemove);
      console.log("鼠标松开了！");
      console.log("移动的距离：", this.moveLen);
    },
    onMousemove(e) {
      // console.log("鼠标移动了！");
      const endX = e.clientX;
      const moveLen = endX - this.startX; // （endx-startx）= 移动的距离
      this.moveLen = moveLen;
      const CurBoxLen = this.curLen - moveLen;
      const leftBoxLen = this.parentBox.clientWidth - CurBoxLen - this.otherBoxWidth;

      // const CurBoxLen = this.curLen + moveLen; // resize[i].left+移动的距离=左边区域最后的宽度
      // const rightBoxLen =
      //   this.$refs.dragBox.clientWidth - CurBoxLen - this.otherBoxWidth; // 右侧宽度=总宽度-左侧宽度-其它盒子宽度
      // // 当最小宽度时，无法继续拖动
      if (CurBoxLen <= 200 || leftBoxLen <= 200) return;
      this.currentBox.style.width = CurBoxLen + "px"; // 当前盒子的宽度
      // this.resizeBox.style.left = CurBoxLen; // 设置左侧区域的宽度
      this.leftBox.style.width = leftBoxLen + "px";
    },

    onMouseDown(e) {
      this.resizeBox = e.target;
      // this.currentBox = this.resizeBox.parentNode; // 当前盒子
      // this.rightBox = this.getNextElement(this.currentBox); // 当前盒子的下个兄弟节点
      // if (!this.rightBox) return;
      // this.curLen = this.currentBox.clientWidth;
      // this.otherBoxWidth =
      //   this.$refs.dragBox.clientWidth -
      //   this.currentBox.clientWidth -
      //   this.rightBox.clientWidth; // 其他盒子的宽度
      // 颜色改变提醒
      this.resizeBox.style.background = "#ff0000";
      // this.startX = e.clientX;
      // document.addEventListener("mousemove", this.onMousemove);
      // document.addEventListener("mouseup", this.onMouseup);
    },
  },
};
</script>
<style lang="less" scoped>
.aside-right {
  /* background-color: #1f222d; */
  border-color: #010208;
  display: flex; /* 使用flex布局 */
  // overflow: auto; /* 防止内容溢出 */
  overflow-y: auto; /* 超出时显示垂直滚动条 */
}
.aside-right-resize {
  position: absolute;
  top: 0;
  left: 0;
  width: 3px;
  height: 100%;
  cursor: ew-resize;
  background-color: #d0d0d0;
}
.aside-right-resize:hover {
  background-color: #a1a1a1;
}
.el-descriptions {
  font-size: 14px;
  background-color: #282d3a;
}
.el-descriptions__body {
  background-color: #282d3a;
  border-radius: 5px;
  border-color: #282d3a;
  color: #ffffff;
}
.el-descriptions__body .el-descriptions__table {
  border-collapse: collapse;
  width: 100%;
  table-layout: fixed;
}
.el-descriptions .is-bordered {
  table-layout: auto;
}
.el-descriptions--mini,
.el-descriptions--small {
  font-size: 12px;
}
.el-descriptions-item__cell {
  border: none !important;
  border-bottom: 1px solid !important;
}
.el-descriptions-item__content {
  word-break: break-word;
  overflow-wrap: break-word;
}
.main-attribute {
  // position: relative;
  font-size: 14px;
  padding: 0 15px 10px 15px;
  padding-top: 0px;
  padding-right: 0px;
  padding-bottom: 10px;
  padding-left: 15px;
  // overflow-y: auto;
  // overflow-x: auto;
  height: calc(100vh - 97px);
  box-sizing: border-box;
  .map-info-container {
    color: #9199af;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
}
.el-table {
  font-size: 14px;
  background-color: #282d3a;
  show-header: false;
}
</style>
